/*
 * Copyright (C) 2011 bus.fm
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.busfm.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Canvas.EdgeType;
import android.preference.PreferenceManager;

/**
 * <p>
 * Title:PrefUtil
 * </p>
 * <p>
 * Description:PrefUtil
 * </p>
 * <p>
 * Copyright (c) 2011 www.bus.fm Inc. All rights reserved.
 * </p>
 * <p>
 * Company: bus.fm
 * </p>
 * 
 * 
 * @author jingguo0@gmail.com
 * 
 */
public class PrefUtil {
    private static Context mContext;

    public static final String P_USER_NAME = "pref.user.name";
    public static final String P_USER_ACCOUNT = "pref.user.account";
    public static final String P_USER_UID = "pref.user.uid";
    public static final String P_USER_PASSWORD = "pref.user.password";
    public static final String P_FIRST_ENTER = "pref.first.enter";
    public static final String P_ISLOGIN = "pref.isLogin";
    public static SharedPreferences sPref;

    public static void init(Context context) {
        if (null == sPref) {

            if (null == mContext) {
                mContext = context;
                sPref = PreferenceManager.getDefaultSharedPreferences(mContext);
            }

        }
    }

    public static void clear() {
        mContext = null;
        sPref = null;
    }

    public static Context getContext() {
        return mContext;
    }

    public static void setUserAccount(Context context, String account) {
        Editor edit = sPref.edit();

        if (!Utilities.isEmpty(account)) {
            edit.putString(P_USER_ACCOUNT, account);
        }

        edit.commit();
    }

    public static String getUserAccount() {
        final String account = sPref.getString(P_USER_ACCOUNT, null);

        if (Utilities.isEmpty(account)) {
            return "";
        } else {
            return account;
        }
    }

    public static void setUserName(Context context, String name) {
        Editor edit = sPref.edit();

        if (!Utilities.isEmpty(name)) {
            edit.putString(P_USER_NAME, name);
        }

        edit.commit();
    }

    public static String getUserName() {
        final String name = sPref.getString(P_USER_NAME, null);

        if (Utilities.isEmpty(name)) {
            return "";
        } else {
            return name;
        }
    }

    public static void setUserID(Context context, String id) {
        Editor edit = sPref.edit();

        if (!Utilities.isEmpty(id)) {
            edit.putString(P_USER_UID, id);
        }

        edit.commit();
    }

    public static String getUserID() {
        final String id = sPref.getString(P_USER_UID, null);

        if (Utilities.isEmpty(id)) {
            return "";
        } else {
            return id;
        }
    }

    public static void setFirstEnter(boolean flag) {
        Editor edit = sPref.edit();
        edit.putBoolean(P_FIRST_ENTER, flag);
        edit.commit();
    }

    public static boolean getFirstEnter() {
        return sPref.getBoolean(P_FIRST_ENTER, true);
    }

    /**
     * 设置是否登录
     * 
     * @param context
     * @param isActive
     */
    public static void setIsLogin(Context context, boolean isLogin) {
        Editor edit = sPref.edit();
        edit.putBoolean(P_ISLOGIN, isLogin);
        edit.commit();
    }

}
